package org.dei.perla.sys.registry.datastructure.dataStructureTestUnit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.dei.perla.sys.registry.datastructure.BTree;
import org.dei.perla.sys.registry.datastructure.BTreeData;
import org.dei.perla.sys.registry.datastructure.BTreeNode;
import org.dei.perla.sys.registry.datastructure.FunctionalityProxyComponent;

import junit.framework.TestCase;

public class TestBtree extends TestCase {

	protected void setUp() throws Exception {
		super.setUp();
	}
	
	String attrType = "temperature";
	private BTree<Integer> testTree = new BTree(attrType);
	private BTreeNode curNode;
	private ArrayList<FunctionalityProxyComponent> fpcList = new ArrayList<FunctionalityProxyComponent>();
	
	int num_elem =5;
	int node_size = 3;
	

	public void testInsertComparableOfTBTreeNodeString() {
		
		//fail("Not yet implemented");
		
		String attrType = "temperature";
		HashMap<String,Comparable> attrMap ;
		BTreeData  dataCur;
		FunctionalityProxyComponent fpcCur;
		FunctionalityProxyComponent fpcTemp =null;
		ArrayList<BTreeData> data = null;
		BTreeNode cur_node = null;
		
		Integer[] fpc_atttr_val = {1,4,6,19,6,14,0,18,15,8,11,2,20,10,11,12,11,3,2,5,2,17,13,2,11};		
		
		for(int i=0;  i<fpc_atttr_val.length;  i++) {
			
			attrMap = new HashMap<String,Comparable>();
			attrMap.put(attrType, fpc_atttr_val[i]);
			attrMap.put("pressure", fpc_atttr_val[i]*2);
			fpcCur  = new FunctionalityProxyComponent(attrMap);
			dataCur = new BTreeData(attrType);
			dataCur.setFPC(fpcCur);
			testTree.insert(dataCur);
			
			data = testTree.getData_In_Subtree(testTree.getRoot());
			
			assertEquals("Missing data: inserted ="+(i+1)+"; but size is: "+data.size(), data.size(), i+1 );
			
			fpcTemp = ((BTreeData)data.get(i)).getFPC();
			
		}
		
		//System.out.println(  testTree.toString());
		
		assertTrue("Attribute type not set", fpcTemp.getAttributesMap().containsKey("temperature"));
		
		data = testTree.selectGreaterThan(new Integer(7));
		
		int[] fpcid = new int[data.size()];
		
		Iterator idat = data.iterator();
		int i=0;
		while( idat.hasNext() ){
			BTreeData btda =(BTreeData) idat.next();
			fpcid[i++] = btda.getFPC().getID();
			assertEquals("data not greater than threshold", 1, btda.getAttrVal().compareTo(7) );
		}
		
		//System.out.println(testTree.toString());
		cur_node = testTree.selectByValue(1);
		
		data = cur_node.getAlldata();
		while(data.size()>0){
			testTree.delete(data.get(0));
		}
		
		System.out.println(testTree.toString());	
		cur_node = testTree.selectByValue(11);
		
		data = cur_node.getAlldata();
		while(data.size()>0){
			testTree.delete(data.get(0));
		}
		
		//System.out.println(testTree.toString());
		
		
		
		/* TODO test remove 
		 * 
		 */
		
		
	}

}
